{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "连接成功\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "from xtquant.xttrader import XtQuantTrader\n",
    "\n",
    "# miniQMT安装路径\n",
    "mini_qmt_path = r'D:\\国金证券QMT交易端\\userdata_mini'\n",
    "# 创建session_id\n",
    "session_id = int(random.randint(100000, 999999))\n",
    "# 创建交易对象\n",
    "xt_trader = XtQuantTrader(mini_qmt_path, session_id)\n",
    "\n",
    "# 启动交易对象\n",
    "xt_trader.start()\n",
    "# 连接客户端\n",
    "connect_result = xt_trader.connect()\n",
    "\n",
    "if connect_result == 0:\n",
    "    print('连接成功')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "from xtquant.xttype import StockAccount\n",
    "\n",
    "# QMT账号\n",
    "account_id = '8886055186'\n",
    "# 创建账号对象\n",
    "acc = StockAccount(account_id)\n",
    "# 订阅账号\n",
    "res = xt_trader.subscribe(acc)\n",
    "# print(account)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "query positions:\n",
      "positions: 13\n",
      "110047.SH 130:13975.0\n",
      "110063.SH 100:8727.2\n",
      "118026.SH 90:8114.22\n",
      "510880.SH 1200:3573.6\n",
      "510500.SH 1200:5599.2\n",
      "512880.SH 10800:8629.2\n",
      "513330.SH 5000:1720.0\n",
      "515080.SH 1400:1974.0\n",
      "517520.SH 3000:3195.0\n",
      "128136.SZ 20:2228.36\n",
      "159707.SZ 3000:1581.0\n",
      "159806.SZ 12200:4892.2\n",
      "159915.SZ 1000:1525.0\n"
     ]
    }
   ],
   "source": [
    "# 查询当日所有的持仓\n",
    "print(\"query positions:\")\n",
    "positions = xt_trader.query_stock_positions(acc)\n",
    "print(\"positions:\", len(positions))\n",
    "\n",
    "if len(positions) != 0:\n",
    "    for i in range(len(positions)):\n",
    "        print(f\"{positions[i].stock_code} {positions[i].volume}:{round(positions[i].market_value, 3)}\")\n",
    "else:\n",
    "    print(f\"{positions[0].stock_code} {positions[0].volume}:{round(positions[0].market_value, 3)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<xtquant.xtpythonclient.XtPosition at 0x186ca4139f0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "positions[0]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python310",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
